package com.foreknow.elm.controller;

import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;

public class UserController {
    /**
     * 根据用户编号与密码查询用户信息
     * @param req
     * @return
     * @throws Exception
     */
    public User getUserByIdByPass(HttpServletRequest req) throws Exception{
        String userId = req.getParameter("userId");
        String password = req.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.getUserByIdByPass(userId, password);
        return user;
    }

    /**
     * 根据用户编号查询用户表返回的行数
     * @param req
     * @return
     * @throws Exception
     */
    public int getUserById(HttpServletRequest req) throws Exception {
        String userId = req.getParameter("userId");
        UserService service = new UserServiceImpl();
        int result = service.getUserById(userId);
        return result;
    }

    public int saveUser(HttpServletRequest req) throws Exception {
        User user = new User();
        String userId = req.getParameter("userId");
        String password = req.getParameter("password");
        String userName = req.getParameter("userName");
        int userSex = Integer.parseInt(req.getParameter("userSex"));
        user.setUserId(userId);
        user.setPassword(password);
        user.setUserName(userName);
        user.setUserSex(userSex);
        UserService service = new UserServiceImpl();
        int result = service.saveUser(user);
        return result;
    }

}
